<?php
/**
 * Created by PhpStorm.
 * User: hehecat
 * Date: 2018/8/30
 * Time: 14:24
 */

namespace app\admin\controller;


use app\admin\model\gameModel;
use think\Controller;
use think\Session;

class Game extends Controller {

    public function game() {
        $adminid = Session::get('adminid');

        $model = new gameModel();
        $result = $model->getGame($adminid);
        $this->assign("list",$result);
        return $this->fetch();
    }

    public function addGame() {
        $adminid = 1;
        $gamename = input("gamename");
        $intro = input("intro");
        $startdate = input("startdate");
        $enddate = input("enddate");
        $gameimg = request()->file('gameimg');

        $model = new gameModel();
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($gameimg){
            $info = $gameimg->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $result = $model->addGame($adminid,$gamename,$info->getSaveName(),$intro,$startdate,$enddate);
                if ($result){
                    $this->success("添加成功");
                }
            }else{
                // 上传失败获取错误信息
                echo $gameimg->getError();
            }
        }else {
            $this->error("请上传图片");
        }
    }

    public function editgame() {
        $gameid = input("gameid");
        $gamename = input("gamename");
        $intro = input("intro");
        $startdate = input("startdate");
        $enddate = input("enddate");
        $gameimg = request()->file('gameimg');

        $model = new gameModel();
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($gameimg){
            $info = $gameimg->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $result = $model->editgame($gameid,$gamename,$info->getSaveName(),$intro,$startdate,$enddate);
                if ($result){
                    $this->success("修改成功");
                }
            }else{
                // 上传失败获取错误信息
                echo $gameimg->getError();
            }
        }else {
            $result = $model->editgame2($gameid,$gamename,$intro,$startdate,$enddate);
            if ($result){
                $this->success("修改成功");
            }else{
                $this->error("修改失败");
            }
        }
    }

    public function deleteGame() {
        $gameid = input("gameid");
        $model = new gameModel();
        $res = $model->deleteGame($gameid);
        return json($res);
    }

}